package cn.com.oceansoft.osc.ms.utils;

import cn.com.oceansoft.osc.ms.config.AppConstant;
import cn.ucox.web.framework.view.ApiResult;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 验证类
 * @create 2017-02-28 17:23
 * @email ganlu@oceansoft.com.cn
 */
public class VerificationUtil {

    /**
     * 手机号码校验
     *
     * @param phone
     * @return
     */
    public static boolean checkPhone(String phone) {
        String PHONE_REGULAR = "^1[3|4|5|7|8]\\d{9}$";        //手机正则表达式
        Pattern p = Pattern.compile(PHONE_REGULAR);
        Matcher matcher = p.matcher(phone);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 简单校验身份证号码
     *
     * @param number
     * @return
     */
    public static boolean checkIdCard(String number) {
        String IDCARD_REGULAR = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
        Pattern p = Pattern.compile(IDCARD_REGULAR);
        Matcher matcher = p.matcher(number);
        if (!matcher.matches()) {
            return false;
        }
        return true;
    }

    //密码强度校验
    public static boolean validatePwd(String pwd) {
        Pattern p = Pattern.compile("[-\\da-zA-Z`=\\\\ ;',./~!@#$%^&*()_+|{}:<>?]{6,40}");//总的
        Pattern p1 = Pattern.compile(".*\\d.*");//含数字
        Pattern p2 = Pattern.compile(".*[a-z].*");//含小写字母
        Pattern p3 = Pattern.compile(".*[A-Z].*");//含大写字母
        Pattern p4 = Pattern.compile(".*[-`=\\\\ ;',./~!@#$%^&*()_+|{}:<>?].*");//含字符
        Matcher matcher = p.matcher(pwd);
        Matcher matcher1 = p1.matcher(pwd);
        Matcher matcher2 = p2.matcher(pwd);
        Matcher matcher3 = p3.matcher(pwd);
        Matcher matcher4 = p4.matcher(pwd);
        if (matcher.matches() && matcher1.matches() && matcher2.matches() && matcher3.matches() && matcher4.matches()) {
            return true;
        }
        return false;
    }

    //获取随机数
    public static String getRandom() {
        Random rd = new Random();
        String n = "";
        int getNum;
        do {
            getNum = Math.abs(rd.nextInt()) % 10 + 48;//产生数字0-9的随机数
            char num1 = (char) getNum;
            String dn = Character.toString(num1);
            n += dn;
        } while (n.length() < 6);
        return n;
    }


}
